package com.reebake.ideal.verify.code.handler;

import com.reebake.ideal.cache.util.CacheUtil;
import com.reebake.ideal.verify.code.core.VerifyCodeStorage;
import com.reebake.ideal.verify.code.entity.VerifyCodeEntity;
import lombok.RequiredArgsConstructor;
import lombok.Setter;

@Setter
@RequiredArgsConstructor
public class DefaultVerifyCodeStorage implements VerifyCodeStorage {

    private static final String CACHE_KEY_PREFIX = "verifyCode:";
    private long verifyCodeExpireIn = 5 * 60 * 1000;

    private String generateCacheKey(String key) {
        return CACHE_KEY_PREFIX + ":" + key;
    }

    @Override
    public void save(VerifyCodeEntity verifyCodeEntity) {
        String sendKey = verifyCodeEntity.getReceiver();
        String cacheKey = generateCacheKey(sendKey);
        long timeout = verifyCodeExpireIn;

        CacheUtil.put(cacheKey, verifyCodeEntity, timeout);
    }

    @Override
    public VerifyCodeEntity get(String sendKey) {
        String cacheKey = generateCacheKey(sendKey);
        return (VerifyCodeEntity) CacheUtil.get(cacheKey);
    }

    @Override
    public void remove(String sendKey) {
        String cacheKey = generateCacheKey(sendKey);
        CacheUtil.remove(cacheKey);
    }

}
